package com.robotserver.orders.common;

import com.robotserver.orders.vo.*;
import com.robotserver.orders.constant.ResponseCode;
import com.robotserver.orders.constant.ResponseMsg;
import com.robotserver.orders.exception.BusinessException;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Auther: jianghan
 * @Date: 2019/11/8 11:26
 * @Description:
 */
public class DateUtil {
    private static int tbkUnitS = 1200;             // 单位时间(20分钟)1200s
    private static int tbRefundUnitS = 43200;       //单位时间(12小时)43200s
    private static int tbRefundUnitS_new = 1200;    //新版全量下载淘宝维权订单，单位时间(20分钟)1200s
    private static int jdUnitS = 1200;              // 单位时间(20分钟)1200s
    private static int vipUnitMS = 1200000;         // 单位时间(20分钟)1200s
    private static int elmUnitS = 1200;              // 单位时间(20分钟)1200s
    private static int dyUnitS = 1200;              // 单位时间(20分钟)1200s

    /**
     * 获取淘宝订单查询的时间范围集合(20分钟为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @return
     */
    public static List<TbkOrderTimeVO> getTbkOrderTimeScope(long begin_time, long end_time){
        return getTbkOrderTimeVOS(begin_time, end_time, tbkUnitS);
    }

    private static List<TbkOrderTimeVO> getTbkOrderTimeVOS(long begin_time, long end_time, int tbkUnitS) {
        if (begin_time>end_time){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        SimpleDateFormat tbkFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<TbkOrderTimeVO> list = new ArrayList<>();
        int scopeCount = (int) ((end_time-begin_time)/ tbkUnitS);
        if (scopeCount==0){
            TbkOrderTimeVO tbkOrderTimeVO = new TbkOrderTimeVO();
            tbkOrderTimeVO.setBeginTime(tbkFormat.format(new Date(begin_time*1000)));
            tbkOrderTimeVO.setEndTime(tbkFormat.format(new Date(end_time*1000)));
            list.add(tbkOrderTimeVO);
        }else {
            Long tempBeginTime = begin_time;
            for (int i=1;i<=scopeCount;i++){
                Long tempEndTime = tempBeginTime+ tbkUnitS;
                TbkOrderTimeVO tbkOrderTimeVO = new TbkOrderTimeVO();
                tbkOrderTimeVO.setBeginTime(tbkFormat.format(new Date(tempBeginTime*1000)));
                tbkOrderTimeVO.setEndTime(tbkFormat.format(new Date(tempEndTime*1000)));
                list.add(tbkOrderTimeVO);
                tempBeginTime = tempEndTime;
            }

            int smallTime = (int) ((end_time-begin_time)% tbkUnitS);
            if (smallTime>0){
                TbkOrderTimeVO tbkOrderTimeVO = new TbkOrderTimeVO();
                tbkOrderTimeVO.setBeginTime(tbkFormat.format(new Date(tempBeginTime*1000)));
                tbkOrderTimeVO.setEndTime(tbkFormat.format(new Date(end_time*1000)));
                list.add(tbkOrderTimeVO);
            }
        }
        return list;
    }

    /**
     * 获取淘宝维权订单查询的时间范围集合(12小时为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @return
     */
    public static List<TbkOrderTimeVO> getTbkOrderRefundTimeScope(long begin_time, long end_time){
        return getTbkOrderTimeVOS(begin_time, end_time, tbRefundUnitS_new);
    }


    /**
     * 获取京东订单查询的时间范围集合(1小时为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @return
     */
    public static List<String> getJdOrderTimeScope(long begin_time, long end_time){
        if (begin_time>end_time){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        SimpleDateFormat jdFormat = new SimpleDateFormat("yyyyMMddHHmm");

        List<String> list = new ArrayList<>();
        String beginHH = jdFormat.format(new Date(begin_time*1000));
        String endHH = jdFormat.format(new Date(end_time*1000));
        list.add(beginHH);

        int count = ((int) ((end_time-begin_time)/60));

        for (int i =1;i<=count;i++){
            Calendar c = Calendar.getInstance();
            c.setTime(new Date(begin_time*1000));
            c.add(Calendar.MINUTE, i);
            Date date = c.getTime(); //结果
            String nowHH = jdFormat.format(date);
            if (!list.contains(nowHH)){
                list.add(nowHH);
            }
        }
        if (!list.contains(endHH)){
            list.add(endHH);
        }
        return new ArrayList<>(list);
    }

    /**
     * 获取京东订单查询的时间范围集合(20分钟为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @return List<JdOrderTimeVO>
     */
    public static List<JdOrderTimeVO> getJdOrderTimeScope_ByAppKey(long begin_time, long end_time){
        if (begin_time>end_time){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        SimpleDateFormat tbkFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<JdOrderTimeVO> list = new ArrayList<JdOrderTimeVO>();
        int scopeCount = (int) ((end_time-begin_time)/ jdUnitS);
        if (scopeCount==0){
            JdOrderTimeVO jdOrderTimeVO = new JdOrderTimeVO();
            jdOrderTimeVO.setBeginTime(tbkFormat.format(new Date(begin_time*1000)));
            jdOrderTimeVO.setEndTime(tbkFormat.format(new Date(end_time*1000)));
            list.add(jdOrderTimeVO);
        }else {
            Long tempBeginTime = begin_time;
            for (int i=1;i<=scopeCount;i++){
                Long tempEndTime = tempBeginTime + jdUnitS;
                JdOrderTimeVO jdOrderTimeVO = new JdOrderTimeVO();
                jdOrderTimeVO.setBeginTime(tbkFormat.format(new Date(tempBeginTime*1000)));
                jdOrderTimeVO.setEndTime(tbkFormat.format(new Date(tempEndTime*1000)));
                list.add(jdOrderTimeVO);
                tempBeginTime = tempEndTime;
            }

            int smallTime = (int) ((end_time-begin_time)% jdUnitS);
            if (smallTime>0){
                JdOrderTimeVO jdOrderTimeVO = new JdOrderTimeVO();
                jdOrderTimeVO.setBeginTime(tbkFormat.format(new Date(tempBeginTime*1000)));
                jdOrderTimeVO.setEndTime(tbkFormat.format(new Date(end_time*1000)));
                list.add(jdOrderTimeVO);
            }
        }
        return list;
    }

    /**
     * 获取饿了么订单查询的时间范围集合(20分钟为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @return List<JdOrderTimeVO>
     */
    public static List<ElmOrderTimeVO> getElmOrderTimeScope_ByAppKey(long begin_time, long end_time){
        if (begin_time>end_time){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        SimpleDateFormat tbkFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<ElmOrderTimeVO> list = new ArrayList<>();
        int scopeCount = (int) ((end_time-begin_time)/ elmUnitS);
        if (scopeCount==0){
            ElmOrderTimeVO elmOrderTimeVO = new ElmOrderTimeVO();
            elmOrderTimeVO.setBeginTime(tbkFormat.format(new Date(begin_time*1000)));
            elmOrderTimeVO.setEndTime(tbkFormat.format(new Date(end_time*1000)));
            list.add(elmOrderTimeVO);
        }else {
            Long tempBeginTime = begin_time;
            for (int i=1;i<=scopeCount;i++){
                Long tempEndTime = tempBeginTime + elmUnitS;
                ElmOrderTimeVO elmOrderTimeVO = new ElmOrderTimeVO();
                elmOrderTimeVO.setBeginTime(tbkFormat.format(new Date(tempBeginTime*1000)));
                elmOrderTimeVO.setEndTime(tbkFormat.format(new Date(tempEndTime*1000)));
                list.add(elmOrderTimeVO);
                tempBeginTime = tempEndTime;
            }

            int smallTime = (int) ((end_time-begin_time)% elmUnitS);
            if (smallTime>0){
                ElmOrderTimeVO elmOrderTimeVO = new ElmOrderTimeVO();
                elmOrderTimeVO.setBeginTime(tbkFormat.format(new Date(tempBeginTime*1000)));
                elmOrderTimeVO.setEndTime(tbkFormat.format(new Date(end_time*1000)));
                list.add(elmOrderTimeVO);
            }
        }
        return list;
    }


    /**
     * 获取拼多多订单查询的时间范围集合(20分钟为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @return
     */
    public static List<PddOrderTimeVO> getPddOrderTimeScope(long begin_time, long end_time){
        if (begin_time>end_time){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }
        List<PddOrderTimeVO> list = new ArrayList<>();
        int scopeCount = (int) ((end_time-begin_time)/ tbkUnitS);
        if (scopeCount==0){
            PddOrderTimeVO pddOrderTimeVO = new PddOrderTimeVO();
            pddOrderTimeVO.setBeginTime(begin_time);
            pddOrderTimeVO.setEndTime(end_time);
            list.add(pddOrderTimeVO);
        }else {
            Long tempBeginTime = begin_time;
            for (int i=1;i<=scopeCount;i++){
                Long tempEndTime = tempBeginTime+ tbkUnitS;
                PddOrderTimeVO pddOrderTimeVO = new PddOrderTimeVO();
                pddOrderTimeVO.setBeginTime(tempBeginTime);
                pddOrderTimeVO.setEndTime(tempEndTime);
                list.add(pddOrderTimeVO);
                tempBeginTime = tempEndTime;
            }

            int smallTime = (int) ((end_time-begin_time)% tbkUnitS);
            if (smallTime>0){
                PddOrderTimeVO pddOrderTimeVO = new PddOrderTimeVO();
                pddOrderTimeVO.setBeginTime(tempBeginTime);
                pddOrderTimeVO.setEndTime(end_time);
                list.add(pddOrderTimeVO);
            }
        }
        return list;
    }

    /**
     * 获取抖音订单查询的时间范围集合(20分钟为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @return List<JdOrderTimeVO>
     */
    public static List<DyOrderTimeVO> getDyOrderTimeScope(long begin_time, long end_time){
        if (begin_time>end_time){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        SimpleDateFormat dyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<DyOrderTimeVO> list = new ArrayList<>();
        int scopeCount = (int) ((end_time-begin_time)/ dyUnitS);
        if (scopeCount==0){
            DyOrderTimeVO dyOrderTimeVO = new DyOrderTimeVO();
            dyOrderTimeVO.setBeginTime(dyFormat.format(new Date(begin_time*1000)));
            dyOrderTimeVO.setEndTime(dyFormat.format(new Date(end_time*1000)));
            list.add(dyOrderTimeVO);
        }else {
            Long tempBeginTime = begin_time;
            for (int i=1;i<=scopeCount;i++){
                Long tempEndTime = tempBeginTime + dyUnitS;
                DyOrderTimeVO dyOrderTimeVO = new DyOrderTimeVO();
                dyOrderTimeVO.setBeginTime(dyFormat.format(new Date(tempBeginTime*1000)));
                dyOrderTimeVO.setEndTime(dyFormat.format(new Date(tempEndTime*1000)));
                list.add(dyOrderTimeVO);
                tempBeginTime = tempEndTime;
            }

            int smallTime = (int) ((end_time-begin_time)% dyUnitS);
            if (smallTime>0){
                DyOrderTimeVO dyOrderTimeVO = new DyOrderTimeVO();
                dyOrderTimeVO.setBeginTime(dyFormat.format(new Date(tempBeginTime*1000)));
                dyOrderTimeVO.setEndTime(dyFormat.format(new Date(end_time*1000)));
                list.add(dyOrderTimeVO);
            }
        }
        return list;
    }

    /**
     * 获取穿山甲联盟抖音订单查询的时间范围集合(20分钟为一个单元时间)
     * @param begin_time        开始时间 (s)
     * @param end_time          结束时间 (s)
     * @return List<JdOrderTimeVO>
     */
    public static List<DyOrderCsjTimeVO> getDyOrderCsjTimeScope(long begin_time, long end_time){
        if (begin_time>end_time){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        SimpleDateFormat dyFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<DyOrderCsjTimeVO> list = new ArrayList<>();
        int scopeCount = (int) ((end_time-begin_time)/ dyUnitS);
        if (scopeCount==0){
            DyOrderCsjTimeVO dyOrderCsjTimeVO = new DyOrderCsjTimeVO();
            dyOrderCsjTimeVO.setBeginTime(begin_time);
            dyOrderCsjTimeVO.setEndTime(end_time);
            list.add(dyOrderCsjTimeVO);
        }else {
            Long tempBeginTime = begin_time;
            for (int i=1;i<=scopeCount;i++){
                Long tempEndTime = tempBeginTime + dyUnitS;
                DyOrderCsjTimeVO dyOrderCsjTimeVO = new DyOrderCsjTimeVO();
                dyOrderCsjTimeVO.setBeginTime(tempBeginTime);
                dyOrderCsjTimeVO.setEndTime(tempEndTime);
                list.add(dyOrderCsjTimeVO);
                tempBeginTime = tempEndTime;
            }

            int smallTime = (int) ((end_time-begin_time)% dyUnitS);
            if (smallTime>0){
                DyOrderCsjTimeVO dyOrderCsjTimeVO = new DyOrderCsjTimeVO();
                dyOrderCsjTimeVO.setBeginTime(tempBeginTime);
                dyOrderCsjTimeVO.setEndTime(end_time);
                list.add(dyOrderCsjTimeVO);
            }
        }
        return list;
    }


    public static int getNowTime(){
        return getTime(new Date());
    }

    public static int getTime(Date date){
        if (date==null){
            return 0;
        }
        return (int) (date.getTime()/1000);
    }

    public static int getStrTime(String date){
        int time = getTime(DateFormatUtil.StringToDateFormat(date));
        if (time<0){
            time = getNowTime();
        }
        return time;
    }

    public static int getStrTime(String date,String dateFormat){
        int time = getTime(DateFormatUtil.StringToDateFormat(date,dateFormat));
        if (time<0){
            time = getNowTime();
        }
        return time;
    }

    public static long getDayBegin() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime().getTime()/1000;
    }

    public static List<VipOrderTimeVO> getVipOrderTimeScope(long begin_time, long end_time) {
        if (begin_time>end_time){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }
        List<VipOrderTimeVO> list = new ArrayList<>();
        long scopeCount = ((end_time-begin_time) / vipUnitMS);
        if (scopeCount==0){
            VipOrderTimeVO vipOrderTimeVO = new VipOrderTimeVO();
            vipOrderTimeVO.setBeginTime(begin_time);
            vipOrderTimeVO.setEndTime(end_time);
            list.add(vipOrderTimeVO);
        }else {
            long tempBeginTime = begin_time;
            for (long i=1;i<=scopeCount;i++){
                Long tempEndTime = tempBeginTime+ vipUnitMS;
                VipOrderTimeVO vipOrderTimeVO = new VipOrderTimeVO();
                vipOrderTimeVO.setBeginTime(tempBeginTime);
                vipOrderTimeVO.setEndTime(tempEndTime);
                list.add(vipOrderTimeVO);
                tempBeginTime = tempEndTime;
            }

            long smallTime = ((end_time-begin_time)% vipUnitMS);
            if (smallTime>0){
                VipOrderTimeVO vipOrderTimeVO = new VipOrderTimeVO();
                vipOrderTimeVO.setBeginTime(tempBeginTime);
                vipOrderTimeVO.setEndTime(end_time);
                list.add(vipOrderTimeVO);
            }
        }
        return list;
    }

    public static void main(String[] args) {
        String str = "2021-03-15 11:29:13";
        getStrTime(str);
    }


}
